/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.core.database;

import com.farmcode.api.plugin.ConfigureException;
import com.farmcode.api.plugin.ObjectInfoPlugin;
import com.farmcode.api.plugin.objectinfo.PluginNotFoundException;
import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.api.runtime.database.DatabaseEngineException;
import com.farmcode.api.runtime.database.DatabaseExplorerAPI;
import com.farmcode.api.runtime.metadata.ObjectInfo;
import com.farmcode.api.util.FARMCodeAPI;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Local;
import javax.ejb.Stateless;

/**
 *
 * @author oiprado
 */
@Local(DatabaseExplorerAPI.class)
@Stateless(mappedName="ejb/databaseExplorer", name="ejb/databaseExplorer")
public class DatabaseExplorerBean implements DatabaseExplorerAPI {

  private List<ObjectInfo> objectsInfo;

  @Override
  public List<ObjectInfo> getObjects(FARMCodeContext context, ObjectInfo type) throws DatabaseEngineException {
      
    objectsInfo = new ArrayList<ObjectInfo>();

        try {
            
            String objectType = (String)context.get(FARMCodeContext.OBJECT_TYPE);
            String pluginManagerName = (String)context.get(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER);
            
            objectsInfo = FARMCodeAPI.getObjectInfoPlugin(objectType, pluginManagerName).getObjectsInfo(context, "");
            
        } catch (PluginNotFoundException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ConfigureException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    return objectsInfo;
  }

  @Override
  public ObjectInfo getColumn(FARMCodeContext context, ObjectInfo type) throws DatabaseEngineException {
        try {
            String objectType = (String)context.get(FARMCodeContext.OBJECT_TYPE);
            String pluginManagerName = (String)context.get(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER);
            
            ObjectInfoPlugin objectInfoPlugin = FARMCodeAPI.getObjectInfoPlugin(objectType, pluginManagerName);
            objectInfoPlugin.getObjectAttributes(context, type);
        } catch (PluginNotFoundException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ConfigureException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
        }      
    return type;
  }

    @Override
    public String test(FARMCodeContext context) {
        try {
            //        try {
            //            try {
            //                return new TablesDatabaseObjectInfo(context).test() + "";
            //            } catch (SQLException ex) {
            //                return ex.getMessage();
            //            }
            //        } catch (ClassNotFoundException ex) {
            //            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
            //        }
            //        }
            String objectType = (String)context.get(FARMCodeContext.OBJECT_TYPE);
            String pluginManagerName = (String)context.get(FARMCodeContext.OBJECT_INFO_PLUGIN_MANAGER);
            return FARMCodeAPI.getObjectInfoPlugin(objectType, pluginManagerName).ping(context);
        } catch (PluginNotFoundException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        } catch (IOException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        } catch (InstantiationException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        } catch (ConfigureException ex) {
            Logger.getLogger(DatabaseExplorerBean.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        }
     
        
    }
}
